package org.sdp.pearproject.service.impl;


import org.sdp.pearproject.dto.convert.*;
import org.sdp.pearproject.dao.domain.*;
import org.sdp.pearproject.dto.request.*;
import org.sdp.pearproject.dto.response.*;
import org.sdp.pearproject.dao.mapper.*;
import org.sdp.pearproject.service.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: IndexServiceSuperImpl
 * @Description:
 * @Author: SDP
 * @Date: 2022-07-25
 * @Version: 1.0
 * Table: index
 * Comment:
 * 
 */
public class IndexServiceSuperImpl extends BaseServiceImpl implements IndexService {
    @Autowired
    protected IndexMapper indexMapper;

    public static Map<String, String> uniqueFieldMap = initUniqueFieldMap();

    public static Map initUniqueFieldMap() {
        HashMap map = new HashMap<>();
        return map;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addIndex(IndexAddRequest request) throws Exception {
        IndexWithBLOBs daoRequest = new IndexAddRequestConvert().convert(request);
        fixEqualCondition(request, "index", "add", "", null);
        Integer result = 0;
        try {
            result = indexMapper.insertSelective(daoRequest);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
        }
        addHistory(request, "A", "index", "api", result, null);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateIndex(IndexUpdateRequest request) throws Exception {

        IndexWithBLOBs daoRequest = new IndexUpdateRequestConvert().convert(request);
        IndexExample example = new IndexExample();
        if (request.getQueryOptions() != null && request.getQueryOptions().containsKey("setnull")) {
            example.setSetNull(JSONObject.toJavaObject(request.getQueryOptions().getJSONObject("setnull"), IndexExample.CriteriaString.class));
        }
        Integer result = 0;
        try {
            result = indexMapper.updateByExampleSelective(daoRequest, example);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            return result;
        }
        throw createUpdateFailException();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteIndex(IndexDeleteRequest request) throws Exception {
        Date now = new Date();
        Integer count = 0;

        BaseRequest abstractRequest = new BaseRequest();
        copyProperties(request, abstractRequest);

        return count;
    }

    @Override
    public IndexQueryResponse queryIndex(IndexQueryRequest request) throws Exception {
        IndexQueryResponse response = new IndexQueryResponse();
        return response;
    }

    @Override
    public List<IndexQueryResponse> listIndex(IndexQueryRequest request) throws Exception {
        IndexWithBLOBs daoRequest = new IndexWithBLOBs();
        copyProperties(request, daoRequest);
        IndexExample example;
        try {
            example = IndexExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        List<IndexWithBLOBs> list;
        try {
            list = indexMapper.selectByExampleWithBLOBs(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        List<IndexQueryResponse> responseList = new ArrayList<>();
        for(IndexWithBLOBs item : list) {
            IndexQueryResponse newItem = new IndexQueryResponse();
            copyProperties(item, newItem);
            responseList.add(newItem);
        }

        return responseList;
    }

    @Override
    public Integer countIndex(IndexQueryRequest request) throws Exception {
        IndexWithBLOBs daoRequest = new IndexWithBLOBs();
        copyProperties(request, daoRequest);
        IndexExample example;
        try {
            example = IndexExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        Integer count;
        try {
            count = indexMapper.countByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        return count;
    }

    @Override
    public BaseStatResponse statIndex(IndexQueryRequest request) throws Exception {
        throw createFailException("not implement");
    }

    @Override
    public List<Object> indexCheckInstall(BaseRequest request) throws Exception {
        throw createFailException("not implement");
    }

}
